const {
  SvipModel
} = require('../../../utils/svip-model.js');
var svipModel = new SvipModel();
const {
  CurrencyShopModel
} = require('../../../utils/currencyshop-model.js')
const currencyShopModel = new CurrencyShopModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSVip: false, //SVIP类型 true:是 false:否
    integerPrice: '0',
    decimalsPrice: '00',
    shopId: "",
    shopName: "",
    shopAddress: "", //门店地址
    currency: {},
    days: 0, //svip天数
    pkgDesc: "", //权益详情
    pkgRemark: "", //使用说明
    validPeriod: "", //月数
    isBigGift: false, //是否显示大礼包弹出层
    couponList: [], //优惠券列表
    giftRemark: "", //大礼包说明
    couponQty: 0, //优惠券数量
    gameCouponList: [], //游戏券列表
    menus: [],
    activeIdx: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    let shopId = ''
    if (options.shopId) {
      shopId = options.shopId; //门店id
    } else {
      const shopInfo = wx.getStorageSync('shopInfo') || {}
      shopId = shopInfo.shopId
    }
    that.setData({
      shopId
    }, function () {
      that.getMemberInfo();
    })
    const merchantId = wx.getStorageSync('merchantId');
    currencyShopModel.queryMenu({
      merchantId,
      moduleTypes: [6]
    }).then(data => {
      const menus = data.modules[0].contents.filter(item => {
        return item.options.nav_type == '1'
      })
      let activeIdx = 0
      menus.forEach((item, index) => {
        if (item.options.nav_redirect_type == '2') {
          activeIdx = index
        }
      })
      this.setData({
        menus,
        activeIdx
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none'
      })
    })
  },

  /**
   * 获取svip会员信息
   */
  getMemberInfo: function () {
    var that = this;
    var param = {};
    param.merchantId = wx.getStorageSync('merchantId'); //商户id
    param.shopId = that.data.shopId; //门店id
    svipModel.svipQuery(param).then(data => {
      var pkgPrice = data.productPackage.pkgPrice.toFixed(2) || '0.00';
      var price = pkgPrice.split(".");
      var orderParam = {};
      orderParam.price = pkgPrice;
      orderParam.productType = "PKG_SVIP";
      orderParam.goodType = "NORMA";
      orderParam.id = data.productPackage.pkgId;
      orderParam.productSku = data.productPackage.pkgCode;
      orderParam.productName = data.productPackage.pkgName;
      orderParam.int = price[0] || '0';
      orderParam.float = price[1] || '00';
      orderParam.img = data.productPackage.pkgImage;
      var couponData = [];
      if (data.memberDiscountCouponDTOList && data.memberDiscountCouponDTOList.length > 0) {
        for (var i = 0; i < data.memberDiscountCouponDTOList.length; i++) {
          var item = data.memberDiscountCouponDTOList[i];
          var list = {};
          list.couponName = item.couponName ? item.couponName : "-";
          list.giftCoinNum = item.promotionAmount ? item.promotionAmount : "0" || "0"; //赠币数
          list.discountNum = item.promotionAmount ? item.promotionAmount.toFixed(1) : "0.0" || "0.0"; //折扣数
          list.promotionAmount = item.promotionAmount ? item.promotionAmount.toFixed(2) : "0.00" || "0.00"; //满减金额
          var promotionAmount = list.promotionAmount.split(".");
          list.integer = promotionAmount[0];
          list.decimals = promotionAmount[1];
          list.couponType = item.couponType;
          list.accumAmount = item.accumAmount ? item.accumAmount : 0; //满额数量
          couponData.push(list);
        }
      }
      var gameCouponList = []
      if (data.gameCouponList && data.gameCouponList[0] != null) {
        gameCouponList = (data.gameCouponList || []).map(item => {
          const split = parseFloat(item.price).toFixed(2).split('.')
          return {
            ...item,
            img: item.imgUrl,
            productName: item.name,
            int: split[0],
            float: split[1]
          }
        })
      }
      that.setData({
        couponList: couponData,
        isSVip: data.isSVip,
        days: data.days || 0,
        pkgDesc: data.productPackage.pkgDesc || '', //权益详情
        pkgRemark: data.productPackage.pkgRemark || '', //使用说明
        // pkgDesc: (data.productPackage.pkgDesc || '').split('\n'), //权益详情
        // pkgRemark: (data.productPackage.pkgRemark || '').split('\n'), //使用说明
        validPeriod: data.productPackage.validPeriod || '0', //月数
        integerPrice: price[0] || '0',
        decimalsPrice: price[1] || '00',
        shopName: data.shopInfo.shopName,
        shopAddress: data.shopInfo.address || '',
        currency: orderParam,
        couponQty: data.couponQty || 0,
        giftRemark: data.productPackage.giftRemark || '',
        gameCouponList: gameCouponList
      });

    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 购买
   */
  goCommitOrder: function (e) {
    var that = this;
    var htmlUrl = '';
    if (that.data.isSVip) {
      htmlUrl = `/pages/svip/member/index?shopId=${that.data.shopId}`
    } else {
      htmlUrl = `/pages/currencyShop/commitOrder/index?shopName=${that.data.shopName}&currency=${JSON.stringify(that.data.currency)}&shopId=${that.data.shopId}&shopAddress=${that.data.shopAddress}&payType=PKG_SVIP`
    }
    wx.navigateTo({
      url: htmlUrl
    })
  },
  /**
   * 查看
   */
  goMember: function () {
    wx.navigateTo({
      url: `/pages/svip/member/index`
    })
  },

  /**
   * 查看大礼包
   */
  getLookPrize: function () {
    var that = this;
    if (that.data.couponQty > 0) {
      that.setData({
        isBigGift: true,
      })
    } else {
      that.setData({
        isBigGift: false,
      })
    }
  },

  /**
   * 查看大礼包
   */
  closeLookPrize: function () {
    this.setData({
      isBigGift: false,
    })
  },

  /**
   * 查看游戏券详情
   */
  goGameCoupon: function () {
    var that = this;
    var currencyData = that.data.gameCouponList[0] || {};
    wx.navigateTo({
      url: `/pages/currencyShop/currencyDetail/index?shopName=${that.data.shopName}&currency=${JSON.stringify(currencyData)}&shopId=${that.data.shopId}&type=1&notBuy=true`
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})